library(tidyverse) # for dplyr etc.
library(haven) # for reading SPSS files and saving Stata files

# Data: Gles 13 (load data and adjust variables for combining with 2017 data)

gles_13 <- read_spss("ZA5716_v1-0-0.sav") # read data
gles_13$religion <- case_when(gles_13$e16 == 1 ~ "evangelic",
                              gles_13$e16 == 2 ~ "catholic",
                              gles_13$e16 > 2 ~ "none",
                              TRUE < 1 ~ NA_character_)
gles_13$Election <- "2013"
gles_13$partei <- gles_13$a1 %>% as_factor
gles_13$bundesland <- gles_13$bundesland %>% as_factor %>% as.character # federal state variable
gles_13$east <- gles_13$bundesland == "Sachsen" | gles_13$bundesland == "Brandenburg" | gles_13$bundesland == "Sachsen-Anhalt" | gles_13$bundesland == "Thueringen" | gles_13$bundesland == "Berlin" | gles_13$bundesland == "Mecklenburg-Vorpommern"
gles_13$links <- gles_13$e21as
gles_13$rechts <- gles_13$e21bs
gles_13$left_right <- ifelse(as.numeric(gles_13$c3) == -99, NA, as.numeric(gles_13$c3))
gles_13$left_right_cdu <- ifelse(as.numeric(gles_13$c4a) == -99, NA, as.numeric(gles_13$c4a))
gles_13$left_right_csu <- ifelse(as.numeric(gles_13$c4b) == -99, NA, as.numeric(gles_13$c4b))
gles_13$left_right_spd <- ifelse(as.numeric(gles_13$c4c) == -99, NA, as.numeric(gles_13$c4c))
gles_13$left_right_fdp <- ifelse(as.numeric(gles_13$c4d) == -99, NA, as.numeric(gles_13$c4d))
gles_13$left_right_linke <- ifelse(as.numeric(gles_13$c4e) == -99, NA, as.numeric(gles_13$c4e))
gles_13$left_right_gruene <- ifelse(as.numeric(gles_13$c4f) == -99, NA, as.numeric(gles_13$c4f))
gles_13$left_right_piraten <- ifelse(as.numeric(gles_13$c4g) == -99, NA, as.numeric(gles_13$c4g))
gles_13$left_right_afd <- ifelse(as.numeric(gles_13$c4h) == -99, NA, as.numeric(gles_13$c4h))
gles_13$female <- as.numeric(gles_13$e2)
gles_13$age <- 2013 - as.numeric(gles_13$e3)
gles_13$mandate <- ifelse(gles_13$kandidaturtyp == 1, "Liste", ifelse(gles_13$kandidaturtyp == 2, "WK", "WK & Liste"))
gles_13$wei_kandi <- as.numeric(gles_13$wei_kandi)
gles_13$top_kulturanpass <- gles_13$c2a
gles_13$top_regwirt <- gles_13$c2b
gles_13$top_weitumwelt <- gles_13$c2c
gles_13$top_banhomo <- gles_13$c2d
gles_13$top_frauvorzug <- gles_13$c2e
gles_13$top_punish <- gles_13$c2f
gles_13$top_sozsec <- gles_13$c2g
gles_13$top_income <- gles_13$c2h
gles_13$top_einwandecon <- gles_13$c2i
gles_13$top_schwanger <- gles_13$c2j
gles_13$gewaehlt <- as.numeric(gles_13$gewaehlt == 1)

# Data: Gles 17 (load data and adjust variables for combining with 2013 data)

gles_17 <- read_spss("ZA6814_v1-0-0.sav")
gles_17$religion <- case_when(gles_17$e14 == 1 ~ "evangelic",
                              gles_17$e14 == 2 ~ "catholic",
                              gles_17$e14 > 2 ~ "none",
                              TRUE < 1 ~ NA_character_)
gles_17$Election <- "2017"
gles_17$partei <- gles_17$a1 %>% as_factor
gles_17$bundesland <- gles_17$bundesland %>% as_factor %>% as.character # federal state variable
gles_17$east <- gles_17$bundesland == "Sachsen" | gles_17$bundesland == "Brandenburg" | gles_17$bundesland == "Sachsen-Anhalt" | gles_17$bundesland == "Thueringen" | gles_17$bundesland == "Berlin" | gles_17$bundesland == "Mecklenburg-Vorpommern"
gles_17$links <- gles_17$e19as
gles_17$rechts <- gles_17$e19bs
gles_17$left_right <- ifelse(as.numeric(gles_17$c3) == -99, NA, as.numeric(gles_17$c3))
gles_17$left_right_cdu <- ifelse(as.numeric(gles_17$c4a) == -99, NA, as.numeric(gles_17$c4a))
gles_17$left_right_csu <- ifelse(as.numeric(gles_17$c4b) == -99, NA, as.numeric(gles_17$c4b))
gles_17$left_right_spd <- ifelse(as.numeric(gles_17$c4c) == -99, NA, as.numeric(gles_17$c4c))
gles_17$left_right_fdp <- ifelse(as.numeric(gles_17$c4d) == -99, NA, as.numeric(gles_17$c4d))
gles_17$left_right_linke <- ifelse(as.numeric(gles_17$c4e) == -99, NA, as.numeric(gles_17$c4e))
gles_17$left_right_gruene <- ifelse(as.numeric(gles_17$c4f) == -99, NA, as.numeric(gles_17$c4f))
gles_17$left_right_afd <- ifelse(as.numeric(gles_17$c4h) == -99, NA, as.numeric(gles_17$c4h))
gles_17$female <- as.numeric(gles_17$geschlecht)
gles_17$age <- 2017 - as.numeric(gles_17$geburtsjahr)
gles_17$mandate <- ifelse(gles_17$kandidaturtyp == 1, "Liste", ifelse(gles_17$kandidaturtyp == 2, "WK", "WK & Liste"))
gles_17$wei_kandi <- as.numeric(gles_17$wei_kandi)
gles_17$top_kulturanpass <- gles_17$c2a
gles_17$top_regwirt <- gles_17$c2b
gles_17$top_weitumwelt <- gles_17$c2c
gles_17$top_banhomo <- gles_17$c2d
gles_17$top_frauvorzug <- gles_17$c2e
gles_17$top_punish <- gles_17$c2f
gles_17$top_sozsec <- gles_17$c2g
gles_17$top_income <- gles_17$c2h
gles_17$top_einwandecon <- gles_17$c2i
gles_17$top_schwanger <- gles_17$c2j
gles_17$gewaehlt <- as.numeric(gles_17$gewaehlt == 2)

# Data: Gles 21 (load data and adjust variables for combining with 2017 data)

gles_21 <- read_spss("ZA7704_v1-0-0.sav") 
gles_21$religion <- case_when(gles_21$e13 == 1 | gles_21$e13 == 2  ~ "evangelic",
                              gles_21$e13 == 3 ~ "catholic",
                              gles_21$e13 > 3 ~ "none",
                              TRUE < 1 ~ NA_character_)
gles_21$Election <- "2021"
gles_21$partei <- gles_21$partei %>% as_factor %>% gsub("�o", "�", .)
gles_21$bundesland <- gles_21$bundesland %>% as_factor %>% as.character %>% gsub("ü", "�", .) # federal state variable
gles_21$east <- gles_21$bundesland == "Sachsen" | gles_21$bundesland == "Brandenburg" | gles_21$bundesland == "Sachsen-Anhalt" | gles_21$bundesland == "Thueringen" | gles_21$bundesland == "Berlin" | gles_21$bundesland == "Mecklenburg-Vorpommern"
gles_21$left_right <- ifelse(as.numeric(gles_21$c5) < 0, NA, as.numeric(gles_21$c5))
gles_21$left_right_cdu <- ifelse(as.numeric(gles_21$c6b) < 0, NA, as.numeric(gles_21$c6b))
gles_21$left_right_csu <- ifelse(as.numeric(gles_21$c6c) < 0 -99, NA, as.numeric(gles_21$c6c))
gles_21$left_right_spd <- ifelse(as.numeric(gles_21$c6d) < 0, NA, as.numeric(gles_21$c6d))
gles_21$left_right_fdp <- ifelse(as.numeric(gles_21$c6e) < 0, NA, as.numeric(gles_21$c6e))
gles_21$left_right_linke <- ifelse(as.numeric(gles_21$c6g) < 0, NA, as.numeric(gles_21$c6g))
gles_21$left_right_gruene <- ifelse(as.numeric(gles_21$c6f) <0, NA, as.numeric(gles_21$c6f))
gles_21$left_right_afd <- ifelse(as.numeric(gles_21$c6h) < 0, NA, as.numeric(gles_21$c6h))
gles_21$female <- as.numeric(gles_21$geschlecht)
gles_21$age <- 2021 - as.numeric(gles_21$geburtsjahr)
gles_21$mandate <- ifelse(gles_21$kandidaturtyp == 1, "Liste", ifelse(gles_21$kandidaturtyp == 2, "WK", "WK & Liste"))
gles_21$wei_kandi <- as.numeric(gles_21$wei_kandi)
gles_21$top_kulturanpass <- 6-gles_21$c2a
gles_21$top_regwirt <- 6-gles_21$c2b
gles_21$top_weitumwelt <- 6-gles_21$c2k
gles_21$top_banhomo <- 6-gles_21$c2d
gles_21$top_frauvorzug <- 6-gles_21$c2e
gles_21$top_punish <- 6-gles_21$c2f
gles_21$top_sozsec <- 6-gles_21$c2g
gles_21$top_income <- 6-gles_21$c2h
gles_21$top_einwandecon <- 6-gles_21$c2i
gles_21$top_schwanger <- 6-gles_21$c2j
gles_21$gewaehlt <- as.numeric(gles_21$gewaehlt == 1)
gles_21$schwangernew <- ifelse(gles_21$c2j<0, NA, gles_21$c2j)
gles_21$schwangernew <- ifelse(gles_21$schwangernew<3,1,0)

# Append Data

gles13 <- select(gles_13, 
                 matches("top_"),
                 Election,
                 partei,
                 bundesland, east, starts_with("left_right"),
                 female, age, mandate, religion, gewaehlt, wei_kandi)

gles17 <- select(gles_17,
                 matches("top_"),
                 Election,
                 partei,
                 bundesland, east, starts_with("left_right"),
                 female, age, mandate, religion, gewaehlt, wei_kandi)

gles21 <- select(gles_21, 
                 matches("top_"),
                 Election,
                 partei,
                 bundesland, east, starts_with("left_right"),
                 female, age, mandate, religion, gewaehlt, wei_kandi)

df <- bind_rows(gles13, gles17, gles21)

# id for each candidates

df$id <- 1:nrow(df)

# turn into df

df <- as.data.frame(df)

# Homo

df$top_banhomo[df$top_banhomo < 0 | df$top_banhomo > 5] <- NA
df$top_schwanger[df$top_schwanger < 0 | df$top_schwanger > 5] <- NA
df$partei[df$partei == "CSU" | df$partei == "CDU"] <- "CDU/CSU"
df$partei[grepl("GRUEN|GR�|Gruen",df$partei)] <- "B'90/Die Gr�nen"
df$top_banhomo <- as.numeric(df$top_banhomo)
df <- df[df$partei != "PIRATEN",]

# Export Data to Stata for simpler Ordinal Logit Analysis

haven::write_dta(df, path = "gles_ccs_data.dta", 
                 version = 15)

# margins SSM

m13 <- read_stata("m13.dta")
m13$Election <- "2013"

m17 <- read_stata("m17.dta")
m17$Election <- "2017"

m21 <- read_stata("m21.dta")
m21$Election <- "2021"

mdata <- bind_rows(m13, m17, m21) %>%
  select("position" = "_predict",
         "pred" = "_margin",
         "lower" = "_ci_lb",
         "upper" = "_ci_ub",
         "party" = "_m1",
         Election)

mdata$party <- case_when(mdata$party == 1 ~ "AfD",
                         mdata$party == 2 ~ "Greens",
                         mdata$party == 3 ~ "CDU/CSU",
                         mdata$party == 4 ~ "Left Party",
                         mdata$party == 5 ~ "FDP",
                         mdata$party == 6 ~ "SPD")

mdata$position <- case_when(mdata$position == 1 ~ "Against SSM",
                         mdata$position == 2 ~ "Neither nor",
                         mdata$position == 3 ~ "For SSM")

mdata %>%
  filter(position != "Neither nor") %>%
  ggplot(aes(x = party,
                  y = pred,
                  ymin = lower,
                  ymax = upper,
                  color = Election,
             shape = Election)) +
  geom_pointrange(position = position_dodge(width = 0.7)) +
  facet_wrap(~ position, ncol = 5, scales = "free_x") +
  coord_flip() +
  theme_bw(base_size = 14) +
  ylab("Probability of Response") +
  scale_y_continuous(breaks = seq(0,1,by = .2),
                     labels = paste(seq(0,1,by = .2))) +
  theme(legend.position = "bottom") +
  xlab(NULL) +
  scale_color_manual(values = c("grey50", "orange", "purple"))

ggsave(file = "plot_ssm.eps", 
       width = 10, 
       height = 5.5)

ggsave(file = "plot_ssm.pdf", 
       width = 10, 
       height = 5.5)

ggsave(file = "plot_ssm.jpeg", 
       width = 10, 
       height = 5.5,
       dpi = 666)

# margins ABORTION

m13 <- read_stata("m13b.dta")
m13$Election <- "2013"

m17 <- read_stata("m17b.dta")
m17$Election <- "2017"

m21 <- read_stata("m21b.dta")
m21$Election <- "2021"

mdata <- bind_rows(m13, m17, m21) %>%
  select("position" = "_predict",
         "pred" = "_margin",
         "lower" = "_ci_lb",
         "upper" = "_ci_ub",
         "party" = "_m1",
         Election)

mdata$party <- case_when(mdata$party == 1 ~ "AfD",
                         mdata$party == 2 ~ "Greens",
                         mdata$party == 3 ~ "CDU/CSU",
                         mdata$party == 4 ~ "Left Party",
                         mdata$party == 5 ~ "FDP",
                         mdata$party == 6 ~ "SPD")

mdata$position <- case_when(mdata$position == 1 ~ "Pro Choice",
                            mdata$position == 2 ~ "Neither nor",
                            mdata$position == 3 ~ "Against Choice")

mdata %>%
  filter(position != "Neither nor") %>%
  ggplot(aes(x = party,
             y = pred,
             ymin = lower,
             ymax = upper,
             color = Election,
             shape = Election)) +
  geom_pointrange(position = position_dodge(width = 0.7)) +
  facet_wrap(~ position, ncol = 5, scales = "free_x") +
  coord_flip() +
  theme_bw(base_size = 14) +
  ylab("Probability of Response") +
  scale_y_continuous(breaks = seq(0,1,by = .2),
                     labels = paste(seq(0,1,by = .2))) +
  theme(legend.position = "bottom") +
  xlab(NULL) +
  scale_color_manual(values = c("grey50", "orange", "purple"))

ggsave(file = "plot_abortion.pdf", 
       width = 10, 
       height = 5.5)

ggsave(file = "plot_abortion.eps", 
       width = 10, 
       height = 5.5)

ggsave(file = "plot_abortion.jpeg", 
       width = 10, 
       height = 5.5,
       dpi = 666)

